/**
 * @author @叶小夏
 * @since 2019/7/29 0029 15:36
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 */
public class Solution {

    private static int[] nums = new int[]{1,3,-1,-3,5,3,6,7};

    public static void main(String[] args) {
        final int[] ints = new Solution().maxSlidingWindow(nums, 2);
        for (int val:
             ints) {
            System.out.println(val);
        }

        final Window window = new Window(2, 3, nums);
        System.out.println(window.findMax());
    }


    private static class Window {
        private int leftIndex;
        private int size;
        private int[] data;

        Window(int leftIndex, int size, int[] data) {
            this.leftIndex = leftIndex;
            this.size = size;
            this.data = data;
        }

        private int findMax() {
            int max = data[leftIndex];
            for (int i = leftIndex + 1; i <= leftIndex + size - 1; i++) {
                if (max < data[i]) {
                    max = data[i];
                }
            }
            return max;
        }

    }

    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums.length == 0) {
            return new int[0];
        }
        int length = nums.length;
        int[] ret = new int[length - k + 1];
        int index = 0;
        while (index <= length - k) {
            ret[index] = new Window(index, k, nums).findMax();
            index++;
        }
        return ret;
    }
}
